<template>
  <el-dialog
    class="subject-status-add-record"
    title="克隆版本"
    width="600px"
    :show-close="true"
    :visible.sync="cloneVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="版本：" prop="cloneSource">
          <el-radio-group v-model="formData.cloneSource">
            <el-radio label="1">DEV</el-radio>
            <el-radio label="2">PRO</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="版本号：" prop="databaseVersion">
          <el-input
            style="width: 80%"
            clearable
            placeholder="请输入"
            maxlength="30"
            show-word-limit
            v-model="formData.databaseVersion"
          >
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    cloneVisible: Boolean,
    projectId: String,
    cloneData: Object,
  },
  data() {
    return {
      rules: {
        cloneSource: [{ required: true, message: '请选择', trigger: 'blur' }],
        databaseVersion: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: this.validatorVersionNum },
        ],
      },
      loading: false,
      defaultFormData: {
        cloneSource: '',
        databaseVersion: '',
      },
      formData: {
        cloneSource: '',
        databaseVersion: '',
      },
    }
  },

  methods: {
    ...mapActions('solutionVersion', ['cloneProjectDatabaseVersion']),

    // 版本号表单校验
    validatorVersionNum(rule, userList, callback) {
      const pattern = /^v\d+(\.\d+){0,2}$/

      const flag = userList.match(pattern)
      if (flag) {
        callback()
      } else {
        callback(new Error('请输入正确格式，如v1.0'))
      }
    },

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleCloneCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate(valid => {
        if (valid) {
          const data = {
            ...this.formData,
            projectId: this.projectId,
            cloneDatabaseId: this.cloneData.id,
            schemeId: this.cloneData.schemeId,
            schemeVersion: this.cloneData.schemeNum,
          }
          this.cloneProjectDatabaseVersion({ data }).then(res => {
            const { code } = res
            if (code === 200) {
              this.$message({
                message: '操作成功!',
                type: 'success',
              })
              this.$refs.formRef.resetFields()
              this.$emit('handleCloneCancel', 'refresh')
            }
            this.loading = false
          })
        } else {
          this.loading = false
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
@import './clone.scss';
</style>
